package com.example.mywiseelves.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mywiseelves.R;
import com.example.mywiseelves.bean.ElvesBean;
import com.example.mywiseelves.dao.ElvesDao;
import com.example.mywiseelves.dao.UserDao;
import com.example.mywiseelves.view.dialog.MyDialog;

import java.util.List;

/**
 * @author ZJK
 * @data 2021 22
 * @description 图鉴列表适配器
 */
public class IllustrateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ElvesBean> elvesBeanList;
    Context context;
    ViewGroup mParent;
    View view;
    AnimationDrawable animationDrawable;
    private MyDialog myElvesDialog;
    //精灵dao对象
    ElvesDao elvesDao;
    //用户dao对象
    UserDao userDao;
    int userPoint;
    LocalBroadcastManager localBroadcastManager;

    public IllustrateAdapter(Context context, List<ElvesBean> elvesBeanList, int userPoint) {
        this.context = context;
        this.elvesBeanList = elvesBeanList;
        elvesDao = new ElvesDao(context);
        userDao = new UserDao(context);
        this.userPoint = userPoint;
        localBroadcastManager = LocalBroadcastManager.getInstance(context);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView elves_image_iv;
        TextView elves_name_tv;
        TextView elves_point_tv;

        public ViewHolder(View view) {
            super(view);
            elves_image_iv = view.findViewById(R.id.item_elves_image_iv);
            elves_name_tv = view.findViewById(R.id.item_elves_name_tv);
            elves_point_tv = view.findViewById(R.id.item_elves_point_tv);
        }
    }

    /**
     * 刷新数据
     */
    public void refreshListData(List<ElvesBean> elvesBeanList, int userPoint) {
        this.elvesBeanList = elvesBeanList;
        this.userPoint = userPoint;
        notifyDataSetChanged();
    }
    /**
     * 翻页刷新页面
     */
    public void  switchPageRefreshListData(List<ElvesBean> elvesBeanList) {
        this.elvesBeanList = elvesBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mParent = parent;
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_illustrate_recyclerview, mParent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ElvesBean elves = elvesBeanList.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        initOnclickListener(viewHolder, elves);
        //清空：
        viewHolder.elves_name_tv.setText("");
        viewHolder.elves_point_tv.setText("");
        viewHolder.elves_image_iv.setImageResource(0);
        viewHolder.elves_image_iv.setBackgroundResource(0);

        String point = "祈愿点:" + elves.getElves_point();
        if (elves.getElves_unlocking() == 0) {
            viewHolder.elves_image_iv.setImageResource(elves.getElves_image());
        } else if (elves.getElves_unlocking() == 1) {
            viewHolder.elves_image_iv.setBackgroundResource(elves.getElves_anim_normal());
            animationDrawable = (AnimationDrawable) viewHolder.elves_image_iv.getBackground();
            animationDrawable.start();
        }
        viewHolder.elves_name_tv.setText(elves.getElves_name());
        viewHolder.elves_point_tv.setText(point);
    }

    @Override
    public int getItemCount() {
        return elvesBeanList.size();
    }

    /**
     * 初始化点击监听
     */
    public void initOnclickListener(ViewHolder viewHolder, ElvesBean elves) {
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (elves.getElves_unlocking() == 0) {
                    openElvesDialog(elves);
                } else if (elves.getElves_unlocking() == 1) {
                    toElvesDetailsActivity(elves);
                }
            }
        });
    }


    /**
     * 点击跳转精灵详情页
     */
    public void toElvesDetailsActivity(ElvesBean elves) {
        Intent intent = new Intent("IllustrateDetailsActivity");
        intent.putExtra("elves_data", elves);
        context.startActivity(intent);
    }

    /**
     * 点击开启解锁精灵弹窗
     */
    public void openElvesDialog(ElvesBean elves) {
        myElvesDialog = new MyDialog(context, R.style.ExitDialog);
        myElvesDialog.setTitle("解锁精灵");
        //设置内容
        myElvesDialog.setMessage("您确定要花费" + elves.getElves_point() + ",解锁" + elves.getElves_name() + "吗？");
        myElvesDialog.setYesOnclickListener("是的", new MyDialog.onYesOnclickListener() {
            @Override
            public void onYesOnclick() {
                updateElvesUnlocking(elves);
                myElvesDialog.dismiss();
            }
        });
        myElvesDialog.setNoOnclickListener("不，等会", new MyDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                myElvesDialog.dismiss();
            }
        });
        myElvesDialog.show();
    }

    /**
     * 检查是否修改修改精灵解锁状态,修改用户点数
     */
    public void updateElvesUnlocking(ElvesBean elves) {
        if (checkPoint(elves)) {
            userPoint = userPoint - elves.getElves_point();
            userDao.userPointUpdate3(userPoint);
            elvesDao.elvesLockingUpdate(elves);
            Intent intent = new Intent("updateIllustrate");
            localBroadcastManager.sendBroadcast(intent);
            Toast.makeText(context, "解锁成功,您的祈愿点还有：" + userPoint + "!", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "您的祈愿点不足，解锁失败！", Toast.LENGTH_LONG).show();
        }

    }

    /**
     * 检查祈愿点
     */
    public boolean checkPoint(ElvesBean elves) {
        return userPoint >= elves.getElves_point();
    }
}
